from django.db import models


class User(models.Model):
    id = models.BigAutoField(primary_key=True)
    password = models.CharField(max_length=200)
    name = models.CharField(max_length=200)
    rid = models.ForeignKey('Role', to_field='id', on_delete=models.PROTECT, db_column='rid')

    class Meta:
        db_table = 'user'
        ordering = ['id']


class Role(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=200)

    class Meta:
        db_table = 'role'
        ordering = ['id']


class Permission(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=200)
    detail = models.CharField(max_length=200)

    class Meta:
        db_table = 'permission'
        ordering = ['id']


class Empower(models.Model):
    rid = models.ForeignKey('Role', to_field='id', on_delete=models.CASCADE, db_column='rid')
    pid = models.ForeignKey('Permission', to_field='id', on_delete=models.CASCADE, db_column='pid')

    class Meta:
        db_table = 'empower'
        constraints = [
            models.UniqueConstraint(fields=['rid', 'pid'], name='empower_id')
        ]
